package com.mediacloud.app.newsmodule.view

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Point
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentActivity
import com.mediacloud.app.assembly.util.*
import com.mediacloud.app.model.interfaces.IPermissionsAgree
import com.mediacloud.app.model.interfaces.PermissionUtil
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.comment.ShareCommentItem
import com.mediacloud.app.reslib.enums.PosterShareBean
import com.mediacloud.app.share.SocialShareControl
import com.mediacloud.app.share.view.SharePopGridWindow
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.io.File

class PosterShareGridPop(context: Context) : SharePopGridWindow(context) {
    var isPosterShareMode = false
    var shareView: PosterShareView
    var shareViewParent: View

    init {
        shareView = contentView.findViewById(R.id.shareView)
        shareViewParent = contentView.findViewById(R.id.shareViewParent)
    }

    override fun getResLayoutId(): Int {
        return R.layout.popwin_share_posterimage
    }

    override fun show(view: Activity) {
        shareView.removeAllViews()
        super.show(view)
    }

    override fun dismiss() {
        shareView.setOnClickListener(null)
        super.dismiss()
    }

    /**
     * 新闻的
     */
    fun updateData(posterShareBean: PosterShareBean, article: ArticleItem) {
        var titlePointTL = posterShareBean.title_coordinate_top_left.split(",")
        var titlePointBR = posterShareBean.title_coordinate_lower_right.split(",")
        val titlePoint = PointView(Point(titlePointTL[0].toInt(), titlePointTL[1].toInt()), Point(titlePointBR[0].toInt(), titlePointBR[1].toInt()), PointType.Title, article.title)

        var contentPointTL = posterShareBean.content_coordinate_top_left.split(",")
        var contentPointBR = posterShareBean.content_coordinate_lower_right.split(",")
//        val contentPoint = PointView(Point(contentPointTL[0].toInt(), contentPointTL[1].toInt()), Point(contentPointBR[0].toInt(), contentPointBR[1].toInt()), if ("pic" != article.share_poster?.type) PointType.Content else PointType.Image
//                , article.share_poster?.value
//                ?:article.logo)

        val contentPoint = PointView(
            Point(contentPointTL[0].toInt(), contentPointTL[1].toInt()), Point(contentPointBR[0].toInt(), contentPointBR[1].toInt()), PointType.Image, article.share_poster?.value
                ?: article.logo
        )

        var qrCodePointTL = posterShareBean.qr_coordinate_top_left.split(",")
        var qrCodePointBR = posterShareBean.qr_coordinate_lower_right.split(",")
        val qrCodePoint =
            PointView(Point(qrCodePointTL[0].toInt(), qrCodePointTL[1].toInt()), Point(qrCodePointBR[0].toInt(), qrCodePointBR[1].toInt()), PointType.QrCode, article.url)


        var pointList = mutableListOf(contentPoint, titlePoint, qrCodePoint)
        shareView.pointView = pointList
        shareView.removeAllViews()
        shareView.postDelayed({
            shareView.loadPosterImage(posterShareBean.back_img)
        }, 300)
    }

    var resized = false
    fun updateSpiderData(posterShareBean: PosterShareBean, article: ArticleItem, spiderView: View) {

        var contentPointTL = posterShareBean.content_coordinate_top_left.split(",")
        var contentPointBR = posterShareBean.content_coordinate_lower_right.split(",")
        val contentPoint =
            PointView(Point(contentPointTL[0].toInt(), contentPointTL[1].toInt()), Point(contentPointBR[0].toInt(), contentPointBR[1].toInt()), PointType.LayoutView, spiderView)

        var qrCodePointTL = posterShareBean.qr_coordinate_top_left.split(",")
        var qrCodePointBR = posterShareBean.qr_coordinate_lower_right.split(",")
        val qrCodePoint =
            PointView(Point(qrCodePointTL[0].toInt(), qrCodePointTL[1].toInt()), Point(qrCodePointBR[0].toInt(), qrCodePointBR[1].toInt()), PointType.QrCode, article.url)

        var pointList = mutableListOf(contentPoint, qrCodePoint)
        shareView.pointView = pointList
        shareView.removeAllViews()
        shareView.postDelayed({
            if (!resized) {
                val scaleNum = 0.87F
                resized = true
                var h = ((shareViewParent.height - shareViewParent.height * scaleNum) / 2).toInt()
                var w = ((shareViewParent.width - shareViewParent.width * scaleNum) / 2).toInt()
                shareViewParent.setPaddingRelative(w, h, w, h)
            }
            /*shareView.scaleX=scaleNum
            shareView.scaleY=scaleNum*/
            shareView.loadPosterImage(posterShareBean.back_img)
        }, 300)
    }

    /**
     *  评论分享
     */
    fun updateData(posterShareBean: PosterShareBean, article: ShareCommentItem) {
        var titlePointTL = posterShareBean.title_coordinate_top_left.split(",")
        var titlePointBR = posterShareBean.title_coordinate_lower_right.split(",")
        val titlePoint = PointView(Point(titlePointTL[0].toInt(), titlePointTL[1].toInt()), Point(titlePointBR[0].toInt(), titlePointBR[1].toInt()), PointType.Title, article.title)

        var contentPointTL = posterShareBean.content_coordinate_top_left.split(",")
        var contentPointBR = posterShareBean.content_coordinate_lower_right.split(",")
        val contentPoint =
            PointView(Point(contentPointTL[0].toInt(), contentPointTL[1].toInt()), Point(contentPointBR[0].toInt(), contentPointBR[1].toInt()), PointType.CommentContent, article)

        var qrCodePointTL = posterShareBean.qr_coordinate_top_left.split(",")
        var qrCodePointBR = posterShareBean.qr_coordinate_lower_right.split(",")
        val qrCodePoint =
            PointView(Point(qrCodePointTL[0].toInt(), qrCodePointTL[1].toInt()), Point(qrCodePointBR[0].toInt(), qrCodePointBR[1].toInt()), PointType.QrCode, article.url)

        var pointList = mutableListOf(titlePoint, contentPoint, qrCodePoint)
        shareView.pointView = pointList
        shareView.removeAllViews()
        shareView.postDelayed({
            shareView.loadPosterImage(posterShareBean.back_img)
        }, 300)
    }

    /**
     * 邀请码
     */
    fun updateInviteData(view: View) {
        val scaleValue = 1.0F
        shareView.removeAllViews()
        val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
       /* val parentWidth = shareViewParent.measuredWidth - dp2px(context, 31)!!.toInt() * 2
        val parentHeight = shareViewParent.measuredHeight - dp2px(context, 22)!!.toInt() * 2
        lp.width = (parentWidth * scaleValue).toInt()
        lp.height = (parentHeight * scaleValue).toInt()*/
        shareView.addView(view, lp)
    }

    fun saveImage() {
        PermissionUtil.invokeStoragePermission(shareViewParent.context as FragmentActivity, object : IPermissionsAgree {
            override fun onAgree() {
                save()
            }

            override fun onRefuse() {
                dismiss()
            }

            override fun hasPermissions() {
                save()
            }
        })

    }

    private fun save() {
        doAsync {
            val bitmap: Bitmap? = BitmapUtil.convertViewToBitmap(shareView) ?: return@doAsync
            val path = FileUtil.getPhotoApp() + "/" + System.currentTimeMillis() + ".png"
            FileUtil.saveBitmap(path, bitmap)
            uiThread {
                val file = File(path)
                Utility.insertIntoMediaStore(contentView.context, false, file, file.lastModified())
                toastCenter(contentView.context, "图片保存成功")
                dismiss()
                bitmap?.recycle()
            }
        }
    }

    fun shareImage(share_media: Int) {
        shareImage(share_media, object : SocialShareControl.SocialShareListener {

            override fun shareComplete(p0: Int, p1: HashMap<String, Any>?) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }

            override fun shareCancel(p0: Int) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }

            override fun shareError(p0: Int, p1: String?) {
                dismiss()
                SocialShareControl.removeSocialShareListener(this)
            }
        })
    }

    fun shareImage(share_media: Int, listener: SocialShareControl.SocialShareListener? = null) {
        PermissionUtil.invokeStoragePermission(shareViewParent.context as FragmentActivity, object : IPermissionsAgree{
            override fun onAgree() {
                realShare(share_media, listener)
            }

            override fun onRefuse() {
            }

            override fun hasPermissions() {
                realShare(share_media, listener)
            }
        })
    }

    private fun realShare(share_media: Int, listener: SocialShareControl.SocialShareListener?) {
//        doAsync {
//            val bitmap = BitmapUtil.convertViewToBitmap(shareView)
//            val path = FileUtil.saveShareBitmap(bitmap)
//            uiThread {
//                SocialShareControl.share(ViewUtils.searchTintContextHostActivity(shareView.context), share_media, path)
//                SocialShareControl.addSocialShareListener(listener)
//            }
//        }
        shareViewParent.let {
            doAsync {
                val bitmap = BitmapUtil.convertViewToBitmap(it)
                val path = FileUtil.saveShareBitmap(bitmap)
                uiThread {
                    SocialShareControl.share(
                            ViewUtils.searchTintContextHostActivity(context),
                            share_media,
                            path
                    )
                    SocialShareControl.addSocialShareListener(object :
                            SocialShareControl.SocialShareListener {

                        override fun shareComplete(platform: Int, p1: HashMap<String, Any>?) {
                            dismiss()
                            SocialShareControl.removeSocialShareListener(this)
                        }

                        override fun shareCancel(platform: Int) {
                            dismiss()
                            SocialShareControl.removeSocialShareListener(this)
                        }

                        override fun shareError(platform: Int, p1: String?) {
                            dismiss()
                            SocialShareControl.removeSocialShareListener(this)
                        }
                    })
                }
            }

        }
    }
}